package com.ymatou.iapi.productmanager.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.GetByProductIdListBean;
import com.ymatou.iapi.productmanager.parameter.GrouponProductListDto;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.product.CatalogSaveModel;
import com.ymatou.iapi.productmanager.service.GetByProductIdListCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.GrouponCatalogs;
import com.ymttest.database.model.GrouponProducts;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*查询商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetByProductIdList{
	private static GetByProductIdListBean getbyproductidlistBean;
	private static GetByProductIdListCall getbyproductidlistCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询商品列表");
	}

	@Before
	public void caseUp() {
		getbyproductidlistBean=new GetByProductIdListBean();
		getbyproductidlistCall=new GetByProductIdListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getbyproductidlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetByProductIdList_001() {
		Logger.start(true,"查询商品列表成功");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			addProdReq.getProduct().getCatalogList().get(0).setPreSale(true);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//手动调同步mongo
			ProductManagerCallServiceV3.createProduct_sync(productId);
			
			List<String> productIdList=Arrays.asList(productId);
			getbyproductidlistBean.setProductIdList(productIdList);
			getbyproductidlistCall.setData(getbyproductidlistBean);
			getbyproductidlistCall.callService();			
			Logger.verifyEquals("200", getbyproductidlistCall.getString("Code"), "验证返回");
			//Thread.sleep(3000);
			verifyDB(addProdReq);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	public void verifyDB(AddSellerProductRequest addProdReq){
		getbyproductidlistCall.getProductList().forEach(p->{
			YmtProductsI ymtProductsI = null;
			try {
				List<YmtProductsI> ymtProductsIs = ymtProductsIWapper.selectProductByProductId(p.getProductId());
				int redo=0;
				while(ymtProductsIs==null||ymtProductsIs.size()<1) {
					Thread.sleep(1000);
					ymtProductsIs = ymtProductsIWapper.selectProductByProductId(p.getProductId());
					redo++;
					Logger.debug("redo:"+redo+":"+FormatUtil.beanToGSONString(ymtProductsIs));
				}
				ymtProductsI = ymtProductsIs.get(0);
				List<YmtCatalogsI> catalogsIS = ymtCatalogsIWapper.selectCatalogsByProductId(p.getProductId());
				List<GrouponProducts> gp = ymtProductsIWapper.selectGrouponProducts(p.getProductId(), null, 0);
				Integer totalStock = 0;
				for (YmtCatalogsI x:catalogsIS
				) {
					totalStock = totalStock+x.getiNum();
				}
				Logger.verifyEquals(p.getSellerId(),sellerId,"验证sellerId");
				Logger.verifyEquals(p.getProductName(),ymtProductsI.getsProduct(),"验证productName");
				Logger.verifyEquals(p.getBrandId(),ymtProductsI.getiBrandId(),"验证BrandId");
				Logger.verifyEquals(p.getCatalogStatus(),ymtProductsI.getiCatalogStatus(),"验证CatalogStatus");
				List<YmtCatalogsI> ymtCatalogsIS = new YmtCatalogsIWapper().selectCatalogsByProductId(p.getProductId());
				double MinCatalogPrice = Collections.min(ymtCatalogsIS.stream().filter(c->c.getiAction()==0).map(c -> c.getfQuotePrice()).collect(Collectors.toList())).doubleValue();
				double MaxCatalogPrice = Collections.max(ymtCatalogsIS.stream().filter(c->c.getiAction()==0).map(c -> c.getfQuotePrice()).collect(Collectors.toList())).doubleValue();
				Logger.verifyEquals(p.getMaxPrice(),MaxCatalogPrice,"验证MaxCatalogPrice");
				Logger.verifyEquals(p.getMinPrice(),MinCatalogPrice,"验证MaxCatalogPrice");
				Logger.verifyEquals(p.getTotalStock(),totalStock,"验证totalStock");
				Logger.verifyEquals(p.getBrandId(),addProdReq.getProduct().getBrandId(),"验证BrandId");
				//这里的BrandName其实是根据BrandId查出来的
				Logger.verifyEquals(p.getBrandName(),addProdReq.getProduct().getBrandName(),"验证tBrandName");
				Logger.verifyEquals(p.getCatalogStatus(),addProdReq.getProduct().getCatalogStatus(),"验证CatalogStatus");
				//SaleStatus 	int	上下架等状态 1上架，2下架，3售罄
//				preSale	boolean	是否包含预订规格
				boolean pre=false;
				List<CatalogSaveModel> clist = addProdReq.getProduct().getCatalogList();
				for (CatalogSaveModel catalogSaveModel : clist) {
					if(catalogSaveModel.isPreSale()==true) {
						pre=true;
					}
				}
				Logger.verifyEquals(p.getPreSale(),addProdReq.getProduct().getCatalogList().get(0).isPreSale(),"验证CatalogStatus");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});
	}
}